﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class BannerBLL
  {
    //Get all
    public List<Banner> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Banners.OrderBy(p => p.Position).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Banner> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Banners.Where(p => p.isEnable == isEnable).OrderBy(p => p.Position).OrderBy(p => p.Sort).ToList();
      }
    }

    //Get by id

    public Banner GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Banners.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public Banner GetById(int id,bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Banners.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    //Get by Position

    public List<Banner> GetByPosition(int position)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Banners.Where(p => p.Position == position).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Banner> GetByPosition(int position, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Banners.Where(p => p.Position == position && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    //Get max Sort

    public byte GetMaxSort(int position)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Banner obj = data.Banners.Where(p => p.Position == position).OrderByDescending(p => p.Sort).FirstOrDefault();

        if (obj == null) return 1;
        else return (byte)(obj.Sort + 1);
      }
    }

    //Swap sort

    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Banner objFirst = data.Banners.Where(p => p.Id == idFirst).FirstOrDefault();
        Banner objSecond = data.Banners.Where(p => p.Id == idSecond).FirstOrDefault();

        byte tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }

    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Banner obj = data.Banners.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Banner obj = data.Banners.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }

    //Add

    public void Add(Banner obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Banners.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    //Update 

    public void Update(Banner obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Banner banner = data.Banners.Where(p => p.Id == obj.Id).FirstOrDefault();
        banner.Image = obj.Image;
        banner.isEnable = obj.isEnable;
        banner.Link = obj.Link;
        banner.Name = obj.Name;
        banner.Position = obj.Position;
        banner.Sort = obj.Sort;

        data.SubmitChanges();
      }
    }

    //Delete

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Banner obj = data.Banners.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
          data.Banners.DeleteOnSubmit(obj);

        data.SubmitChanges();
      }
    }
  }
}
